package com.houdoudou.com.machine;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;

public class MachineManage {

	public static	JSONObject  manageMachine(String jsonData){
		JSONObject result = new JSONObject();
		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
		//判断请求的功能
		int FunctionCode=jsonfrom.getInt("FunctionCode");
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		   // 此值代表用户身份，不同的身份，对于同一个增删改查函数，有不同的处理，防止出错。
		   int Manager_Authority= jsonfrom.getInt("Manager_Authority");
		switch(FunctionCode){
		// 查询 
		case 1:{
			
				
				// step2 查询字管理员
				  
				   int Machine_ID=jsonfrom.getInt("Machine_ID");
				   try {
					   String sql = "select * from Tb_Machine where Machine_ID="+Machine_ID+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
						if(rs.next()){

								result.put("ActionStatus", true);
								result.put("ErrorInfo", "查询成功");						
								result.put("Machine_ID", rs.getInt("Machine_ID"));
								result.put("Owner_Telnum", rs.getString("Owner_Telnum"));
								result.put("Machine_Address", rs.getString("Machine_Address"));						
								result.put("Machine_Longitude", rs.getDouble("Machine_Longitude"));							
								result.put("Machine_Latitude", rs.getDouble("Machine_Latitude"));
								result.put("Machine_Time_M", rs.getString("Machine_Time_M"));
								result.put("Machine_Time_N", rs.getString("Machine_Time_N"));
								result.put("Machine_Time_E", rs.getString("Machine_Time_E"));
								result.put("Machine_Time_Config", rs.getString("Machine_Time_Config"));
								result.put("Machine_Temp_Config", rs.getString("Machine_Temp_Config"));
								result.put("Machine_Secret", rs.getString("Machine_Secret"));
								result.put("Machine_Update_No", rs.getInt("Machine_Update_No"));
								result.put("Parent_Manager_Telnum", rs.getString("Parent_Manager_Telnum"));
								result.put("Machine_Name", rs.getString("Machine_Name"));
								///////
								
							
								
							
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "要查询的用户不存在");
						}
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "数据库查询异常");
					}    			
				   break;     } //case 1
		//修改
		case 2:{
			
			// step2 数据库输入。
			 
			try {
				   int Machine_ID=jsonfrom.getInt("Machine_ID");
					
				   String sql = "select * from Tb_Machine where Machine_ID="+Machine_ID+"";  
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
					rs= stm.executeQuery(sql);
					if(rs.next()){		
						 Manager_Authority=jsonfrom.getInt("Manager_Authority");
						switch(Manager_Authority){
						case 1:{
							//step1 查询新业主是否存在。
							   String Owner_New_Telnum=jsonfrom.getString("Owner_Telnum");
							   
							   String sql2="select * from Tb_Owner where Owner_Telnum="+Owner_New_Telnum+"";  
							ResultSet rscheck=stm.executeQuery(sql2);
							//更改机器的业主，对应管理员也要一并改成业主对应的管理员。
							if(rscheck.next()){
							rs.updateString("Owner_Telnum", Owner_New_Telnum);	
							 rs.updateString("Parent_Manager_Telnum", rscheck.getString("Parent_Manager_Telnum"));
							//  更新完吃数据后，所有用户的本地信息都需要更新。
							rs.updateInt("Machine_Update_No", rs.getInt("Machine_Update_No")+1);
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
							}else{
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "新业主不存在");
							}
							
							CommonFunction.closeConn(null, null, null, rscheck);
						           break;}
						case 2:{
							rs.updateString("Machine_Secret", jsonfrom.getString("Machine_Secret"));
							
										
							//  更新完吃数据后，所有用户的本地信息都需要更新。
							rs.updateInt("Machine_Update_No", rs.getInt("Machine_Update_No")+1);
							
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						                break;}
						case 3:{
						       if(rs.getString("Parent_Manager_Telnum").equals(jsonfrom.getString("Manager_Telnum"))){
							
						    rs.updateString("Machine_Address", jsonfrom.getString("Machine_Address"));
							rs.updateDouble("Machine_Longitude", jsonfrom.getDouble("Machine_Longitude"));
						    rs.updateDouble("Machine_Latitude", jsonfrom.getDouble("Machine_Latitude"));
							rs.updateString("Machine_Time_M", jsonfrom.getString("Machine_Time_M"));
							rs.updateString("Machine_Time_N", jsonfrom.getString("Machine_Time_N"));
							rs.updateString("Machine_Time_E", jsonfrom.getString("Machine_Time_E"));
						    rs.updateString("Machine_Temp_Config", jsonfrom.getString("Machine_Temp_Config"));	
						    rs.updateString("Machine_Time_Config", jsonfrom.getString("Machine_Time_Config"));
						   //  更新完吃数据后，所有用户的本地信息都需要更新。
							rs.updateInt("Machine_Update_No", rs.getInt("Machine_Update_No")+1);
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						         }else{
						        	 result.put("ActionStatus", false);
										result.put("ErrorInfo", "更新失败，不能更改其他区域经理下的业主");
						         }
						        break;}
						// 代表业主自己可以修改机器信息。
						case 4:{
							
						    rs.updateString("Machine_Address", jsonfrom.getString("Machine_Address"));
							rs.updateDouble("Machine_Longitude", jsonfrom.getDouble("Machine_Longitude"));
							rs.updateDouble("Machine_Latitude", jsonfrom.getDouble("Machine_Latitude"));
						    rs.updateString("Machine_Time_M", jsonfrom.getString("Machine_Time_M"));
							rs.updateString("Machine_Time_N", jsonfrom.getString("Machine_Time_N"));
							rs.updateString("Machine_Time_E", jsonfrom.getString("Machine_Time_E"));
						    rs.updateString("Machine_Temp_Config", jsonfrom.getString("Machine_Temp_Config"));
						    rs.updateString("Machine_Time_Config", jsonfrom.getString("Machine_Time_Config"));
							//  更新完吃数据后，所有用户的本地信息都需要更新。
							rs.updateInt("Machine_Update_No", rs.getInt("Machine_Update_No")+1);
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						
						                break;}
						
						}
							
														
										
							
				
					}else{
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "要修改的用户不存在");
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "数据库查询异常");
			}
			
			 break;    } //case2
		// 删除
				case 3:{	
					
					if(Manager_Authority==2){
						 int Machine_ID=jsonfrom.getInt("Machine_ID");
						   try {
							   String sql = "select * from Tb_Machine where Machine_ID="+Machine_ID+"";
								stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
								rs= stm.executeQuery(sql);
								if(rs.next()){
									try{
										
										sql="delete from Tb_Machine where Machine_ID="+Machine_ID+"";		   
							             stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);  							           
							            		 stm.executeUpdate(sql);  	
							     				
													result.put("ActionStatus", true);
													result.put("ErrorInfo", "删除成功");
										} catch (SQLException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
											result.put("ActionStatus", false);
											result.put("ErrorInfo", "数据库删除操作失败");
										} 							
								}else{
									result.put("ActionStatus", false);
									result.put("ErrorInfo", "要删除的用户不存在");
								}
								
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "数据库查询操作失败");
							}   
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "你没有权限");
				}
						   break;     } //case 3
		// 添加管理员 只有区域经理才有此权限。
		case 4:{
			 if(Manager_Authority==3){
						
			   try {
				
				    
				// step2 检查改业主是否属于这个分区经理，
				   String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
				   String  Parent_Manager_Telnum=jsonfrom.getString("Manager_Telnum");
				   String sql = "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"and Parent_Manager_Telnum="+Parent_Manager_Telnum+"";   
				   stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
				   System.out.println(sql);
				   rs= stm.executeQuery(sql);
				   if(rs.next()){
			// step3 数据库输入。
				 
			
				sql = "insert  into Tb_Machine (Owner_Telnum,Machine_Address,Machine_Longitude,Machine_Latitude,Machine_Time_M,Machine_Time_N,Machine_Time_E,Machine_Time_Config,Machine_Temp_Config,Machine_Secret,Machine_Update_No,Parent_Manager_Telnum) values (?,?,?,?,?,?,?,?,?,?,?,?)";
              	pstm=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				pstm.setString(1, jsonfrom.getString("Owner_Telnum"));
				pstm.setString(2, jsonfrom.getString("Machine_Address"));
				
				pstm.setDouble(3, jsonfrom.getDouble("Machine_Longitude"));
				pstm.setDouble(4, jsonfrom.getDouble("Machine_Latitude"));
				pstm.setString(5, jsonfrom.getString("Machine_Time_M"));
				
		
				pstm.setString(6, jsonfrom.getString("Machine_Time_N"));
				pstm.setString(7, jsonfrom.getString("Machine_Time_E"));
				pstm.setString(8, jsonfrom.getString("Machine_Time_Config"));
				pstm.setString(9, jsonfrom.getString("Machine_Temp_Config"));
				pstm.setString(10, jsonfrom.getString("Machine_Secret"));
				pstm.setInt(11, jsonfrom.getInt("Machine_Update_No"));
				pstm.setString(12, jsonfrom.getString("Parent_Manager_Telnum"));
			    pstm.executeUpdate();
			    result.put("ActionStatus", true);
				result.put("ErrorInfo", "增加成功");
				rs=pstm.getGeneratedKeys();
				if(rs.next())
				result.put("Machine_ID", rs.getInt(1));
				}else{
					 result.put("ActionStatus", false);
						result.put("ErrorInfo", "业主不存在，或者业主不属于此分区经理");
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "数据库异常");
			}
			   
			 }// end of if manager_authority==3
			 else{
				 result.put("ActionStatus", false);
					result.put("ErrorInfo", "你没有权限操作");
			 }
			   
			   break;    } //case4
		
		
		//查询所有机器
		  case 5:{
			  try {
				 		 
				   String sql = "select * from Tb_Machine";  
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
					rs= stm.executeQuery(sql);
					JSONArray Machines= new JSONArray();
					while(rs.next()){
						JSONObject machine= new JSONObject();
						boolean isadd=false;
					switch(Manager_Authority){
					
					case 1:{	isadd=true;
					              break;}
					case 2:{	isadd=true;
		                          break;}
					case 3:{
						if(rs.getString("Parent_Manager_Telnum").equals(jsonfrom.getString("Manager_Telnum"))) 
							isadd=true;
						        break;}				
		             
					case 4:{
						if(rs.getString("Owner_Telnum").equals(jsonfrom.getString("Owner_Telnum"))) 
							isadd=true;
						        break;}				
		             
					 }
					if(isadd){
						machine.put("Owner_Telnum", rs.getString("Owner_Telnum"));
                        machine.put("Machine_ID", rs.getString("Machine_ID"));
                        machine.put("Machine_Time_Config", rs.getString("Machine_Time_Config"));
                        machine.put("Parent_Manager_Telnum", rs.getString("Parent_Manager_Telnum"));
                         Machines.add(machine);               
					                         }
					
					}
					result.put("ActionStatus", true);
					result.put("ErrorInfo", "成功");
					result.put("Machines", Machines);
			   
			  }catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "查询失败");
				} 
					
					
				 break;  } //case5
		
		} //switch
		
               CommonFunction.closeConn(conn, pstm, stm, rs);
		
		
		
		return result;
	}
	
	
	
	 JSONObject  loginCheck(String jsonData){
		 // step1 所有数据定义
		   JSONObject result= new JSONObject();
	
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		 // step2 数据解析 	  
		   JSONObject json= JSONObject.fromObject(jsonData);
		   String Manager_Telnum=json.getString("Manager_Telnum");
		   String Manager_Secret=json.getString("Manager_Secret");
		   int Manager_Authority= json.getInt("Manager_Authority");
		   try {
		   String sql = "select * from Tb_Parent_Manager where Manager_Telnum="+Manager_Telnum+"";  
			stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
			rs= stm.executeQuery(sql);
			if(rs.next()){
				String correctSecret= rs.getString("Manager_Secret");
				if(correctSecret.equals(Manager_Secret)){
				String Ahourity=rs.getString("Manager_Authority"); 
				CharSequence isAhouritied=Ahourity.subSequence(Manager_Authority-1, Manager_Authority);
				if(isAhouritied.charAt(0)=='1'){
					String isManager_Authority= rs.getString("Manager_isAuthority");
					if(isManager_Authority.equals("1")){
					result.put("ActionStatus", true);
					result.put("ErrorInfo", "登陆成功");
					}else{
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "该用户没有被授权");
					}
					
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "角色不对");
				}
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "密码不正确");
				}
			}else{
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "不存在该用户");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		   
		   CommonFunction.closeConn(conn, pstm, stm, rs);
		   return result;
	 }
	
	

	
}
